:- module(tsv_mapping,[]).


:- use_module('../ns').
:- use_module('../lib/t_record').
:- use_module('../lib/tsv').
:- use_module('../p2r/match').


/**
 * Some general purpose predicates, used
 * in the following mapping
 */

:- dynamic files/1.


directory(Dir,URI) :-
	tsv_file(File),
	tsv_file_to_id(File,T),
	ns:item_ns(NS),
	tsv:value(File,Dimensions,_,Indice,_,_),
	dimension_id(Dimensions,DimID),
	format(atom(Dir),'data/~w/~w_~w',[T,Indice,DimID]),
	format(atom(URI),'~w~w/~w_~w',[NS,T,Indice,DimID]).

tsv_file(File) :-
	files(TSV),!,
	member(File,TSV).
tsv_file(File) :-
	expand_file_name('data/all_data/*.tsv',TSV),
	assert(files(TSV)),
	member(File,TSV).

tsv_file_to_id(File,ID) :-
	file_base_name(File,Base),
	atom_concat(ID,'.tsv',Base).


dimension_id(Dimensions,Id) :-
	findall(Dim,member(dimension(_,Dim),Dimensions),Dims),
	concat_atom(Dims,'_',Id).

format_label(Id,Dimensions,Indice,IndiceDim,Label) :-
	findall(Dim,member(dimension(_,Dim),Dimensions),Dims),
	concat_atom(Dims,' ',LD),
	format(atom(Label),'Table: ~w, dimensions: ~w, ~w: ~w',[Id,LD,IndiceDim,Indice]).



/**
 * Mapping values, indices and dimensions for each item (each entry in a eurostat tsv table)
 */
match:
	(
	 ns:item_ns(NS),
	 ns:dim_ns(DimNS),
	 tsv_mapping:tsv_file(File),
	 tsv_mapping:tsv_file_to_id(File,Id),
	 tsv:value(File,Dimensions,IndiceDim,Indice,Value,''), % no flag
	 tsv_mapping:dimension_id(Dimensions,DimID),
	 tsv_mapping:format_label(Id,Dimensions,Indice,IndiceDim,Label),
	 DimID\=''
	)
		eq
	[
		rdf(pattern([NS,Id,'/',Indice,'_',DimID]),rdf:value,literal(Value))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),riese:dimension,pattern([DimNS,IndiceDim,'/',Indice]))
	,	rdf(pattern([NS,Id]),riese:datasetOf,pattern([NS,Id,'/',Indice,'_',DimID]))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),riese:dataset,pattern([NS,Id]))
	, rdf(pattern([NS,Id,'/',Indice,'_',DimID]),dc:title,literal(Label))
	, rdf(pattern([NS,Id,'/',Indice,'_',DimID]),rdfs:label,literal(Label))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),rdf:type,riese:'Item')
	].

/**
 * Mapping dimensions for each item
 */
match:
	(
	 ns:dim_ns(DimNS),
	 ns:item_ns(NS),
	 tsv_mapping:tsv_file(File),
	 tsv_mapping:tsv_file_to_id(File,Id),
	 tsv:value(File,Dimensions,_,Indice,_,_),
	 member(dimension(Dic,Dim),Dimensions),
	 tsv_mapping:dimension_id(Dimensions,DimID),
	 DimID\=''
	)
		eq
	[
		rdf(pattern([NS,Id,'/',Indice,'_',DimID]),riese:dimension,pattern([DimNS,Dic,'/',Dim]))
		% NO BACKLINKs... too much possible results
	].

/**
 * Mapping values, indices and dimensions for each item (each entry in a eurostat tsv table)
 * If the actual value is flagged
 */
match:
	(
	 ns:flag_ns(FlagNS),
	 ns:item_ns(NS),
	 ns:dim_ns(DimNS),
	 tsv_mapping:tsv_file(File),
	 tsv_mapping:tsv_file_to_id(File,Id),
	 tsv:value(File,Dimensions,IndiceDim,Indice,Value,F),F\='', %  flag
	 tsv_mapping:dimension_id(Dimensions,DimID),
	 tsv_mapping:format_label(Id,Dimensions,Indice,IndiceDim,Label),
	 DimID\=''
	)
		eq
	[
	 	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),rdf:value,literal(Value))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),riese:flag,pattern([FlagNS,F])) %NO BACKLINK
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),riese:dimension,pattern([DimNS,IndiceDim,'/',Indice]))
	,	rdf(pattern([NS,Id]),riese:datasetOf,pattern([NS,Id,'/',Indice,'_',DimID]))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),riese:dataset,pattern([NS,Id]))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),dc:title,literal(Label))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),rdfs:label,literal(Label))
	,	rdf(pattern([NS,Id,'/',Indice,'_',DimID]),rdf:type,riese:'Item')
	].


/**
 * Mapping eurostat datasets
 */
match:
	(
	 ns:item_ns(NS),
	 tsv_mapping:tsv_file(File),
	 tsv_mapping:tsv_file_to_id(File,Id),
	 once(t_record:t_record(File,[Struct|_]))
	)
		eq
	[
		rdf(pattern([NS,Id]),riese:structure,literal(Struct))
	%,	rdf(pattern([NS,Id]),rdfs:label,literal(Id))
	%,	rdf(pattern([NS,Id]),rdf:type,riese:'Dataset')
	].


